#include "paint.h"
//------------------------------------布置背景----------------------------
void paintBackground()
{
    
    for(int i=1;i<=100;i=i+2) //i代表行
    {
        for(int j=1;j<=50;j++) //j代表列
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xD) ;
            if(i==1||i==99||j==1||j==50)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xF) ;
            }
            COORD coord;
            coord.X = i;  //第3行
            coord.Y = j;  //第3列
            //获取控制台缓冲区句柄
            HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
            //设置光标位置
            SetConsoleCursorPosition(ConsoleHandle, coord);
            puts("?");
        }  
    } 
}

//--------------------------------设置出发点----------------------------
void paintStart()
{
    coorD.X = 49;  
    coorD.Y = 25;  
    corrD_1.X = 48;
    corrD_1.Y = 25;
    corrD_2.X = 47;
    corrD_2.Y = 25;
    //获取控制台缓冲区句柄
    HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    //设置光标位置
    SetConsoleCursorPosition(ConsoleHandle, coorD);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1) ;
    puts("@");
    SetConsoleCursorPosition(ConsoleHandle, corrD_1);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1) ;
    puts("@");
    SetConsoleCursorPosition(ConsoleHandle, corrD_2);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1) ;
    puts("@");  
}

//--------------------------------键盘监听----------------------------
void getInput()
{
    char ch;
    int i = 0;
    //循环监听，直到按Esc键退出
    while(1)
    {
        if(kbhit())
        {  //检测缓冲区中是否有数据
            ch = getch();  //将缓冲区中的数据以字符的形式读出
            switch (ch)
            {
                case 97:  corrD_W = corrD_2;corrD_2 = corrD_1;corrD_1 = coorD;coorD.X =  coorD.X - 1; break;  //a：左
                case 0x73:corrD_W = corrD_2;corrD_2 = corrD_1;corrD_1 = coorD;coorD.Y =  coorD.Y + 1; break;  //s：下
                case 119: corrD_W = corrD_2;corrD_2 = corrD_1;corrD_1 = coorD;coorD.Y =  coorD.Y - 1; break;  //w：上
                case 100: corrD_W = corrD_2;corrD_2 = corrD_1;corrD_1 = coorD;coorD.X =  coorD.X + 1; break;  //d：右
            }
            HANDLE ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
            SetConsoleCursorPosition(ConsoleHandle, corrD_W);
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xD) ;
            puts("?");
            SetConsoleCursorPosition(ConsoleHandle, corrD_1);
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1) ;
            puts("@");
            SetConsoleCursorPosition(ConsoleHandle, corrD_2);
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1) ;
            puts("@");  
            SetConsoleCursorPosition(ConsoleHandle, coorD);
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1) ;
            puts("@");  
        } 
        //Sleep(1000);  //暂停1秒
    }
}
    

